package team.se.tms.controller.teacher;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.enums.ResultEnum;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.TeacherInfo;
import team.se.tms.service.TeacherInfoService;
import team.se.tms.util.ResultUtil;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 教师信息控制层
 *
 * @author qingx
 * @date 2018/11/28
 */
@Slf4j
@Controller
@RequestMapping("teacher")
public class TeacherInfoController extends BaseController{

    @Autowired
    public TeacherInfoService teacherInfoService;


    @RequestMapping("list")
    public String list() throws Exception{
        return "teacher/teacher_list";
    }

    /**
     * 查询教师列表
     *
     * @param page
     * @return
     * @throws Exception
     * @create on 2018/11/28 by qingx
     */
    @RequestMapping("getList")
    @ResponseBody
    public String searchAllTeacherInfos(Page page,HttpSession httpSession) throws Exception{
        System.out.println("page.getOrderDir()"+page.getOrderDir());
        List<TeacherInfo> teacherInfos =  teacherInfoService.listAllTeacherInfos(page);
        httpSession.setAttribute("teacherInfos",teacherInfos);
        System.out.println("page.getDataTabelJson"+page.getDataTabelJson(teacherInfos));
        return page.getDataTabelJson(teacherInfos);
    }

    /**
     * 跳转到添加教师页面
     *
     * @return
     * @throws Exception
     * @create on 2018/11/28 by qingx
     */
    @RequestMapping("toAddTeacher")
    public String toAddTeacher() throws Exception{
        return "teacher/add_teacher";
    }

    /**
     * 添加教师信息
     *
     * @param addType 添加教师类型
     * @param teacherInfo 教师信息
     * @return
     * @throws Exception
     * @create on 2018/12/2 by qingx
     */
    @RequestMapping(value = "addTeacherInfo",method = RequestMethod.POST)
    @ResponseBody
    @CheckFormat({"userId"})
    public Object addTeacherInfo(String addType,TeacherInfo teacherInfo) throws Exception{
        System.out.println("111111111teacherInfo"+teacherInfo);
        String userIdReturn = teacherInfoService.savaTeacherInfo(addType,teacherInfo);
        if(userIdReturn == null){
            throw new InsertException("教师录入失败");
        }else if (userIdReturn != null && addType.equals("addNewUser")){
            return  ResultUtil.success("教师录入成功，用户id为"+userIdReturn);
        }else if (userIdReturn != null && addType.equals("addExistingUser")){
            return  ResultUtil.success("教师录入成功");
        }
        throw new InsertException("教师录入失败");
    }

    /**
     * 跳转到修改教师页面
     *
     * @return
     * @throws Exception
     * @create on 2018/12/6 by qingx
     */
    @RequestMapping("toModityTeacher")
    public ModelAndView toModityTeacher(@Regex("^[A-Za-z0-9]{16}$")String teaId, HttpSession session) throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<TeacherInfo> teacherInfos = (List<TeacherInfo>)session.getAttribute("teacherInfos");
        TeacherInfo teacherInfo = new TeacherInfo();
        for(TeacherInfo item:teacherInfos){
            if(item.getTeaId().equals(teaId)){
                teacherInfo = item;
                break;
            }
        }
        System.out.println("!!!!!!!!!!!!!!!!!!teacherInfo"+teacherInfo);
        //将教师信息返回给界面
        mv.addObject("teacherInfo",teacherInfo);
        mv.setViewName("teacher/modify_teacher");
        return mv;
    }

    /**
     * 修改教师信息
     *
     * @param teacherInfo 教师信息
     * @return
     * @throws Exception
     * @create on 2018/12/7 by qingx
     */
    @RequestMapping(value = "editTeacherInfo",method = RequestMethod.POST)
    // @CheckFormat({"labelId"})
    @ResponseBody
    @CheckFormat
    public Object editTeacherInfo(TeacherInfo teacherInfo) throws Exception{
        System.out.println("111111111teacherInfo"+teacherInfo);
        boolean result = teacherInfoService.modifyTeacherInfo(teacherInfo);
        if(result == true){
            return  ResultUtil.success("教师修改成功");
        }
        throw new UpdateException("教师修改失败");
    }
    /**
     * 查询个人信息
     *
     * @param session 参数描述
     * @return ModelAndView 教师信息和返回的界面
     * @throws Exception 异常
     * @create on 2018/12/28 by sunddly
     */
    @RequestMapping("teacher_info")
    public ModelAndView viewTeacherInfo(HttpSession session) throws Exception{
        ModelAndView mv =this.getModelAndView();
        TeacherInfo teacherInfo =teacherInfoService.getTeacherInfo("20181208232425ON");
        if(teacherInfo!=null){
            mv.addObject("result",ResultUtil.success());
        }else{
            //System.out.println("^^^^^^^^^^"+ResultUtil.error(ResultEnum.SELECT_ERROR.getCode()));
            mv.addObject("result",ResultUtil.error(ResultEnum.SELECT_ERROR.getCode()));
        }
        mv.addObject("teacherInfo",teacherInfo);
        mv.setViewName("teacher/tea_my_info");
        return mv;
    }
}
